//
// Created by heqian on 19-4-7.
//

#include "WindowMain.h"
#include "res.h"

WindowMain::WindowMain() {
    set_title("Ngrok 配置");
    set_position(WIN_POS_CENTER);
    set_resizable(false);

    Glib::ustring window(_binary_window_glade_start, _binary_window_glade_end - _binary_window_glade_start);
    Glib::RefPtr<Builder> builder = Builder::create_from_string(window);
    mBoxMain = Glib::RefPtr<Box>::cast_static(builder->get_object("BoxMain"));
    add(*mBoxMain.get());

    mEntryUser = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryUser"));
    mEntryPass = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryPass"));

    mComboxHttp = Glib::RefPtr<ComboBoxText>::cast_static(builder->get_object("ComboxHttp"));
    mEntryHttpAdd = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryHttpAdd"));
    mEntryHttpPort = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryHttpPort"));

    mComboxHttps = Glib::RefPtr<ComboBoxText>::cast_static(builder->get_object("ComboxHttps"));
    mEntryHttpsAdd = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryHttpsAdd"));
    mEntryHttpsPort = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryHttpsPort"));

    mComboxTcp = Glib::RefPtr<ComboBoxText>::cast_static(builder->get_object("ComboxTcp"));
    mEntryTcpAdd = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryTcpAdd"));
    mEntryTcpPort = Glib::RefPtr<Entry>::cast_static(builder->get_object("EntryTcpPort"));

    Glib::RefPtr<Button>::cast_static(builder->get_object("ButtonExit"))->signal_clicked().connect(
            sigc::mem_fun(this, &WindowMain::on_exit_clicked));

    Glib::RefPtr<Button>::cast_static(builder->get_object("ButtonSave"))->signal_clicked().connect(
            sigc::mem_fun(this, &WindowMain::on_save_clicked));

    Glib::RefPtr<Button>::cast_static(builder->get_object("ButtonRun"))->signal_clicked().connect(
            sigc::mem_fun(this, &WindowMain::on_run_clicked));

    mConfig.read("config.yml");
    mEntryUser->set_text(mConfig.getUserName());
    mEntryPass->set_text(mConfig.getPassWord());

    mEntryHttpAdd->set_text(mConfig.getHttpAdd());
    mEntryHttpPort->set_text(mConfig.getHttpPort());

    mEntryHttpsAdd->set_text(mConfig.getHttpsAdd());
    mEntryHttpsPort->set_text(mConfig.getHttpsPort());

    mEntryTcpAdd->set_text(mConfig.getTcpAdd());
    mEntryTcpPort->set_text(mConfig.getTcpPort());

    show_all();
}

void WindowMain::on_exit_clicked() {
    close();
}

void WindowMain::on_save_clicked() {
    save();
}

std::string get_dir() {
#define FILEPATH_MAX (512)
    char *file_path_getcwd;
    file_path_getcwd = (char *) malloc(FILEPATH_MAX);
    getcwd(file_path_getcwd, FILEPATH_MAX);

    const std::string path(file_path_getcwd);
    free(file_path_getcwd);
    return path;
}


void WindowMain::on_run_clicked() {
    save();

    std::string ngrok("deepin-terminal -x bash -c \"");
    ngrok += get_dir();
    ngrok += "/ngrok -config=";
    ngrok += get_dir();
    ngrok += "/config.yml start http\"";

    system(ngrok.c_str());

    close();
}

void WindowMain::save() {
    mConfig.setUserName(mEntryUser->get_text());
    mConfig.setPassWord(mEntryPass->get_text());

    mConfig.setHttpAdd(mEntryHttpAdd->get_text());
    mConfig.setHttpPort(mEntryHttpPort->get_text());

    mConfig.setHttpsAdd(mEntryHttpsAdd->get_text());
    mConfig.setHttpsPort(mEntryHttpsPort->get_text());

    mConfig.setTcpAdd(mEntryTcpAdd->get_text());
    mConfig.setTcpPort(mEntryTcpPort->get_text());

    mConfig.save("config.yml");
}